(function() {
    $.fn.extend({
        Seamlesstimedrotation() {
            var self = this
            var imageindex = 0
            var indexx = 0
            var time = setInterval(() => {
                imageindex++
                indexx++
                if (imageindex > 4) {
                    imageindex = 1
                    this.find(".screen ul").css("left", 0)
                }
                if (indexx >= 4) {
                    indexx = 0
                }
                this.find(".screen").children().animate({
                    left: imageindex * -1519
                })
                this.find("ol li").eq(indexx).addClass("current").siblings().removeClass("current")
            }, 1500)

            //复制第一张图片到最后
            this.find(".screen ul").append(this.find(".screen ul li").eq(0).clone(true))
            $("<li>1</li><li>2</li><li>3</li><li>4</li>").appendTo(this.find("ol"))
            this.find("ol li").eq(0).addClass("current").siblings().removeClass("current")

            this.find("ol li").on("click", function() {
                // clearInterval(time)
                imageindex = $(this).index()
                indexx = imageindex
                self.find(".screen").children().stop().animate({
                    left: imageindex * -1519
                })
                $(this).addClass("current").siblings().removeClass("current")
            })

            this.on("mouseenter", function() {
                clearInterval(time)

            }).on("mouseleave", function() {
                time = setInterval(() => {
                    imageindex++
                    indexx++
                    if (imageindex > 4) {
                        imageindex = 1
                        self.find(".screen ul").css("left", "0")
                    }
                    if (indexx >= 4) {
                        indexx = 0
                    }
                    self.find(".screen").children().stop().animate({
                        left: imageindex * -1519
                    })
                    self.find("ol li").eq(indexx).addClass("current").siblings().removeClass("current")
                }, 1500)

            })

        }
    })






    $(function() {
        $("#all").Seamlesstimedrotation()
    })












    $(function() {
        $(".all-category ul li").on("mouseenter", function() {
            //获取下标
            var $index = $(this).index()
            $(this).addClass("current").siblings().removeClass("current")
            $(".Commodity-classis").children().eq($index).addClass("main").siblings().removeClass("main")
        })

        $(".Commodity-classis").children().on("mouseleave", function() {
            $(".Commodity-classis").children().removeClass("main")
            $(".all-category ul li").removeClass("current")
        })





        //楼层

        $(".Floornavigation ul li").on("click", function() {
            //获取下标
            var $index = $(this).index()
            var $margin = $(".margin").eq($index).offset().top
            console.log($margin)
            $(this).addClass("Floornavigations").siblings().removeClass("Floornavigations")
            $("html,body").animate({
                scrollTop: $margin
            })
        })


        $(".ReturnD").on("click", function() {
            $("html,body").animate({
                scrollTop: 0
            })
        }).on("mouseenter", function() {
            $(this).css("background", "red")
        }).on("mouseleave", function() {
            $(this).css("background", "")
        })




        //滚动条事件
        $(window).on("scroll", _.debounce(function() {
            if ($(this).scrollTop() > 1000) {
                $(".Floornavigation").show()
            } else {
                $(".Floornavigation").hide()
            }
            var itemsList = [...$(".margin")].map(item => {
                    return $(item).offset().top + $(item).height()
                })
                // console.log(itemsList);
            var scrollTop = $(this).scrollTop(); //获取当前的不可见区域的高度
            var index = itemsList.findIndex(item => scrollTop + 200 < item)
            $(".Floornavigation ul li").eq(index).addClass("Floornavigations").siblings().removeClass("Floornavigations")
        }, 100))

        // $(window).triggerHandler('scroll')   



        $("li").on("mouseenter", function() {
            $(this).css("color", "#dd5352")
        }).on("mouseleave", function() {
            $(this).css("color", "")
        })

        $("li").children().on("mouseenter", function() {
            $(this).css("color", "#dd5352")
        }).on("mouseleave", function() {
            $(this).css("color", "")
        })

        $(".classification-external li img").on("mouseenter", function() {
            $(this).css("opacity", "0.8")
        }).on("mouseleave", function() {
            $(this).css("opacity", "1")
        })

        $(".kk1").on("click", function() {
            location = "./Detail page.html?id=001"

        })
        $(".kk2").on("click", function() {
            location = "./Detail page.html?id=002"

        })

        $(".kk3").on("click", function() {
            location = "./Detail page.html?id=003"

        })
        $(".kk4").on("click", function() {
            location = "./Detail page.html?id=004"

        })
        $(".kk5").on("click", function() {
            location = "./Detail page.html?id=005"

        })



        //首页加入购物车

        //获取本地缓存数据
        $(".center-Shoppingcart").on("mouseenter", function() {
            $(".center-Shoppingcart2").show()

            var ousertorage = JSON.parse(localStorage.getItem("currentList") || '[]')
            console.log(ousertorage.uid)
            var obj = {
                id: ousertorage.uid
            }
            $.ajax({
                url: "http://127.0.0.1:8080/addcart/slect",
                type: "get",
                data: obj
            }).then(function(res) {
                var { msg, code, data } = res
                var str = ``

                for (var i = 0; i < data.length; i++) {
                    console.log(data[i])
                    str += `
                <li>
                <img src="${data[i].c_img}" alt="">
                <p class="aclss">${data[i].c_name} </p>
                <p class="aclss2">¥<span>${data[i].c_price}</span> X <strong>${data[i].c_num}</strong></p>
            </li>
                `
                    $(".Shoppingcartjs span").html(data[i].c_price)
                }
                $(".Shoppingcartul").html(str)


            })
        }).on("mouseleave", function() {
            $(".center-Shoppingcart2").hide()

        })

        $(".center-Shoppingcart2").on("mouseenter", function() {
            $(this).show()
        }).on("mouseleave", function() {

            $(this).hide()

        })


        //首页点击登录
        $(".Headnavigation-Edition ul ").find("li").eq(0).on("click", function() {
                location = "./logon.html"
            })
            //首页点击注册
        $(".Headnavigation-Edition ul ").find("li").eq(1).on("click", function() {
            location = "./registern.html"
        })

        $(".Shoppingcartjs button").on("click", function() {
            location = "./Shopping cart page.html"
        })



        var ouserList = JSON.parse(localStorage.getItem("currentList")) || '[]'
        console.log(ouserList.uname)
        if (ouserList.uname == undefined) {
            $(".Headnavigation-Edition ul ").find("li").eq(0).children("span").html("请登录")
        } else {
            $(".Headnavigation-Edition ul ").find("li").eq(0).children("span").html(ouserList.uname)
        }















    })














})()